<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\Repository;

use App\Model\AreaModel;
use Hyperf\Di\Annotation\Inject;

class AreaRepository extends BaseRepository
{
    /**
     * @Inject
     * @var AreaModel
     */
    protected $model;

    /**
     * Notes: 获取地址信息 向上递推
     * Author: Bruce.z
     * DateTime: 2022/7/13 15:44
     * @param int $area_id
     * @return string
     */
    public function address(int $area_id):string
    {
        $re = $this->getOne(['id'=>$area_id], ['p_id','name']);
        $names[] = $re['name'];
        if($re['p_id'] > 0){
            $two = $this->getOne(['id'=>$re['p_id']], ['p_id','name']);
            $names[] = $two['name'];
            if($two['p_id'] > 0){
                $three = $this->getOne(['id'=>$two['p_id']], ['name']);
                $names[] = $three['name'];
            }
        }
        return implode(array_reverse(array_filter($names)), '');
    }
}
